package Servlet;


import Pojo.Journalism;
import Pojo.NewInfo;
import Service.ViceCheck;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Check")
public class Check extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("passwd");
        String phone   = req.getParameter("phone");
        ViceCheck viceCheck = new ViceCheck();
        viceCheck.regist(username,password,phone);
        resp.sendRedirect("login.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        设置一个常量，作为翻页的计数器
        int i =1;
        req.getSession().setAttribute("index",i);

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("userName");
        String password = req.getParameter("passwd");
        ViceCheck viceCheck = new ViceCheck();
//        密码正确
        if (viceCheck.check(username,password)) {
            req.getSession().setAttribute("username", username);
//            进入管理员页面
//            将其信息进行查询，然后保存到新闻类中，然后将其显示在页面上
            Journalism journalism =viceCheck.Journalisminfo();
            req.getSession().setAttribute("journalism",journalism);
//            查询新闻的内容等信息，保存到session中

            int index = (int) req.getSession().getAttribute("index");
            List<NewInfo> newInfo = viceCheck.findinfo(index);
            req.getSession().setAttribute("newInfo",newInfo);
            resp.sendRedirect("index.jsp");

        }else{
            resp.getWriter().print("账户或者密码输入错误！！！");
        }

    }
}
